const Utils = ecodeSDK.imp(Utils)
const GField = ecodeSDK.imp(GField)

const {
	isBlank,
	getForm,
	forEachDetail
} = Utils

const GEvent = {

	// 主表合并到主表
	JOIN_MAIN: 'JOIN_MAIN',
	// 明细表合并到明细表 | 明细表合并到主表
	JOIN_DETAIL: 'JOIN_DETAIL',

	joinField(fieldMarks, target, mode=this.JOIN_MAIN) {
		const { fieldId, detailId } = GField.getFieldInfo(target)
		const arr = fieldMarks.split(',').filter(x => !isBlank(x))
		const form = getForm()
		const handler = (o, rowIdx) => {
			let newVal = ''
			if (/field\d/.test(rowIdx)) rowIdx = null
			arr.forEach(fieldMark => {
				const tmpVal = GField.getFieldValue(fieldMark, rowIdx)
				if (tmpVal !== '') {
					const val = newVal ? `,${tmpVal}` : tmpVal
					newVal += val
				}
			})
			const valSet = new Set(newVal.split(','))
			const value = [...valSet].join(',')
			const fId = rowIdx ? `${fieldId}_${rowIdx}` : fieldId
			form.changeFieldValue(fId, { value })
		}
		GForm.bindFieldChangeEvent(fieldMarks, handler)
		if (mode === this.JOIN_MAIN) {
			handler('', null)
		} else {
			forEachDetail(detailId, idx => handler('', idx))
		}
	},

	bindFieldChangeEvent(fieldMarks, callback) {
		if (isBlank(fieldMarks)) return;
		const form = getForm()
		const handler = fieldMark => {
			const {isDetail, fieldId, detailId} = GField.getFieldInfo(fieldMark)
			if (!isDetail) {
				form.bindFieldChangeEvent(fieldId, callback);
				return
			}
			form.bindDetailFieldChangeEvent(fieldId, callback);
			// 延迟针对字段联动增加行操作
			form.registerAction(form.ACTION_ADDROW + detailId, idx => setTimeout(() => {
				const val = form.getFieldValue(`${fieldId}_${idx}`)
				callback(fieldId, idx, val)
			}, 100))
		};
		fieldMarks.split(',').forEach(fieldMark => {
			fieldMark = fieldMark.trim()
			if (!fieldMark) return
			handler(fieldMark)
		})
	},

	bindFieldCheckEvent(fieldMark, checkFunc) {
		if (isBlank(fieldMark)) return
		const {isDetail, fieldId, detailId} = GField.getFieldInfo(fieldMark)
		const form = getForm()
		let lock = false
		const clear = rowIdx => {
			lock = true
			const fid = isDetail ? `${fieldId}_${rowIdx}` : fieldId
			form.changeFieldValue(fid, {
				value: '',
				specialobj:[
					{id: '', name: ''}
				],
				showhtml: ''
			})
		}
		this.bindFieldChangeEvent(fieldMark, (a, b, v) => {
			if (lock) return lock = false
			checkFunc(a, b, v, () => {}, () => clear(b))
		})

		const handler = callback => {
			if (!isDetail) {
				// 主表字段校验
				const dom = document.querySelector(`input[name="${fieldId}"]`)
				const val = GField.getFieldValue(fieldId)
				checkFunc(dom, fieldId, val, callback, () => clear(fieldId))
				return
			}
			// 明细表字段考研
			let arr = []
			// 明细数据写入数组队列
			forEachDetail(detailId, rowIdx => arr.push({
				rowIdx,
				val: form.getFieldValue(`${fieldId}_${rowIdx}`)
			}))
			const executeVerify = () => {
				let obj
				if ((obj = arr.pop())) {
					// 队列存在数据
					const { rowIdx, val } = obj
					checkFunc(fieldId, rowIdx, val, executeVerify, () => clear(rowIdx))
				} else {
					// 队列不存在数据，则提交
					callback()
				}
			}
			executeVerify()
		}
		// 提交前校验
		if ('registerCheckEvent' in form) {
			// 流程提交前校验
			form.registerCheckEvent(form.OPER_SUBMIT, handler)
		} else {
			// 建模提交前校验
			let lock  = false
			const {Actions} = ModeForm.getCurrentModeStore()
			const callback = Actions.doSubmit
			window.checkCustomize = () => {
				if (!lock) {
					handler(() => {
						lock = true
						callback()
					})
				}
				return lock
			}
		}
	}
}

ecodeSDK.exp(GEvent)